package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Resource
    private CategoryFeignClient categoryFeignClient;

    @Resource
    private TrackInfoFeignClient trackInfoFeignClient;

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        // 创建map集合对象
        HashMap<String, Object> map = new HashMap<>();
        //	将新增的商品SKUID存入布隆过滤器
        //	获取布隆过滤器，
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        // 判断是否存在 不存在则返回空对象
        if (!bloomFilter.contains(albumId)){
            return map;
        }
        // 一共需要返回哪些数据
        //异步编排  加快响应速度
        // albumId 获得albumInfo
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(albumInfoResult, "远程调用专辑服务获取专辑数据失败");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "远程调用专辑服务获取专辑数据albumInfo失败");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        },threadPoolExecutor).exceptionally(ex -> {
            log.error(ex.getMessage());
            return new AlbumInfo();
        });
        // 获取分类信息
        CompletableFuture<Void> baseCategoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<BaseCategoryView> categoryViewResult = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryViewResult, "远程调用专辑服务获取专辑分类数据失败");
            BaseCategoryView baseCategoryView = categoryViewResult.getData();
            Assert.notNull(baseCategoryView, "远程调用专辑服务获取专辑分类数据baseCategoryView失败");
            map.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor).exceptionally(ex -> {
            log.error(ex.getMessage());
            return null;
        });
        // 获取统计信息
        CompletableFuture<Void> albumStatisticsCompletableFuture = CompletableFuture.runAsync(() -> {
            Result<AlbumListVo> albumStatListResult = albumInfoFeignClient.getAlbumStatistics(albumId);
            Assert.notNull(albumStatListResult, "远程调用专辑服务获取专辑统计数据失败");
            AlbumListVo albumStatistics = albumStatListResult.getData();
            Assert.notNull(albumStatistics, "远程调用专辑服务获取专辑统计数据albumStatistics失败");
            map.put("albumStatVo", albumStatistics);
        }, threadPoolExecutor).exceptionally(ex -> {
            log.error(ex.getMessage());
            return null;
        });
        // 获取主播信息
        CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "远程调用用户服务获取用户数据失败");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "远程调用用户服务获取用户数据userInfoVo失败");
            map.put("announcer", userInfoVo);
        }, threadPoolExecutor).exceptionally(ex -> {
            log.error(ex.getMessage());
            return null;
        });


        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                baseCategoryViewCompletableFuture,
                albumStatisticsCompletableFuture,
                announcerCompletableFuture
        ).join();
        return map;
    }
}